# encoding: utf-8
__author__ = 'wwj'
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8')
sys.path.append("/Users/wwj/Downloads/shangtianjun/girl/girl/dbShell")
from dbshell import dbshell

class main(dbshell):
    orderItem = [
            ['name', ''],
            ['tel', ''],
            ['level', ''],
            ['price', ''],
            ['cover', ''],
            ['area', ''],
            ['address', ''],
            ['entrance', ''],
            ['ispark', ""],
            ['roomnum', ""],
            ['rest', ""],
            ['servicetime', ""],
            ['stay', ""],
            ['appointment', ""],
            ['tag', []],
            ['equipment', []],
            ['url', ""],
            ['city', ""],
            ['town', 0],
            ['areatitle', ""],
            ['prix', ""],

    ]
    mongoTable = "hotel"
    mysqlTable = "hotel"
    # urlpoolkey = "exist"
    def __init__(self,spidername):
        super(main,self).__init__(spidername)
        tmp = self.getdb("select id,name,tid from hotelarea")
        self.area = [(x["name"],x["id"],x["tid"]) for x in tmp]
        # self.cate = {x.split()[1]:x.split()[0] for x in tmp}
        # self.geturltool("select distinct(url) from hotel")
        # self.where = {"url":"http://lovehotel-navi.jp/n/hotelmimi/hotel_top/"}
        ########http://lovehotel-navi.jp/n/keibajyouhoteruyu-zu/hotel_top/"}
        self.mongofrom = self.mongo.find().sort([("_id",-1)])


    def clean(self,y):
        data = self.data
        # if y["url"].find("/h/") > 0:
        #     data["area"] = self.area["北海道"]
        # elif y["url"].find("/h/") > 0:
        #     data["area"] = self.area["北海道"]
        for x,yy,z in self.area:
            if y["area"].find(x) == 0:
                data["city"] = yy
                data["area"] = z
            if y["area"].find(x) > 0:
                data["town"] = yy

        key = ""
        tmp = [x for x in y["desc"] if not re.match(r"\s+",x)]
        for x in tmp:
            if key:
                data[key] = x
                key = ""
            if x == "ルーム数":
                key = "roomnum"
            elif x == "駐車場":
                key = "ispark"
            elif x == "アクセス":
                key = "entrance"
            elif x == "休憩":
                key = "rest"
            elif x == "サービスタイム":
                key = "servicetime"
            elif x == "宿泊":
                key = "stay"
            elif x == "予約":
                key = "appointment"
            # elif x == "サーtビス":
            #     import ipdb;ipdb.set_trace()
            #     key = "tag"
            # elif x == "設備":
            #     import ipdb;ipdb.set_trace()
            #     key = "equipment"
        key = ""
        for x in tmp:
            if key:
                data.setdefault(key,[]).append(x)
            if x == "サービス":
                key = "tag"
            elif x == "設備":
                key = "equipment"
        if data["tag"]:
            data["tag"] = ",".join(data["tag"])
        if data["roomnum"]:
            data["roomnum"] = data["roomnum"].replace("室","").replace("-","")
        if data["equipment"]:
            data["equipment"] = ",".join(data["equipment"])
        if data.get("appointment") and data.get("appointment") == "不可":
            data["appointment"] = 0
        else:
            data["appointment"] = 1

        if data.get("ispark") and data.get("ispark") == "-":
            data["ispark"] = 0
        else:
            data["ispark"] = 1

        data["name"] = y["name"]
        data["areatitle"] = y["area"]
        data["tel"] = y["tel"]
        data["price"] = "".join(y["price"])
        # if data["price"]:
        #     import ipdb;ipdb.set_trace()
        try:
            if y["price"].index(u"\xa0\u5bbf\u6cca") >= 0:
                data["prix"] = y["price"][y["price"].index(u"\xa0\u5bbf\u6cca") + 1]
                data["prix"] = data["prix"].replace(",","")
                data["prix"] = re.search("\d+",data["prix"]).group()
        except:
            pass

        # data["prix"] = "".join(y["price"])
        data["level"] = y["level"]
        data["cover"] = y["cover"]
        data["address"] = "".join(y["address"])
        data["url"] = y["url"].replace("http://lovehotel-navi.jp","")



if __name__ == '__main__':
    # while 1:
    #     i = 1
        obj = main("hotel")
        obj()
        # i += 1
